package com.icoolkj.learn.juc.future;

import com.icoolkj.learn.juc.base.utils.SleepTools;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/03/05 17:15
 */
@Slf4j
public class CompletableFutureDemo2 {

    public static void main(String[] args) {
        // 任务1：洗水壶、烧开水
        CompletableFuture<Void> f1 = CompletableFuture.runAsync(()->{
            log.info("T1:洗水壶...");
            SleepTools.second(1);

            log.info("T1:烧开水...");
            SleepTools.second(15);
        });

        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(()->{
            log.info("T2:洗茶壶...");
            SleepTools.second(1);

            log.info("T2:洗茶杯...");
            SleepTools.second(2);

            log.info("T2:拿茶叶...");
            SleepTools.second(1);
            return "太平猴魁";
        });

        CompletableFuture<String> f3 = f1.thenCombine(f2, (t1Result, tf) ->{
            log.info("T1:拿到茶叶：{}", tf);
            log.info("T1:泡茶...");
            return "上茶:" + tf;
        });
        //等待任务3执行结果
        log.info(f3.join());

    }

}
